//引入模型
require('./../model/collectionsModel');
const mongoose = require('mongoose');


//获取所有的收藏题
async function getAllCollections(req,res,next) {
    // let studentId = "610bb559a50c000097006402";
    // let collections = await mongoose.model('collectionsModel').find({studentId:studentId}).populate("exerciseId");
    let collections = await mongoose.model('collectionsModel').find({studentId:req.body}).populate("exerciseId");
    console.log("获取所有收藏题");
    console.log(collections);
    res.send({ data:collections, code: 200 });
}
//删除收藏题
async function removeCollection(req,res,next) {
    let {deletedCount} = await mongoose.model('collectionsModel').deleteOne({_id:req.body});
    if(deletedCount>0){
        res.send({code:200})
    }else{
        res.send({code:-1})
    }
}
//新增收藏题
async function addCollection(req,res,next){
    // let exerciseId = "610bcb96a50c000097006422";
    // let studentId = "61132f3cdf28b3438c760cbf";
    // let data = await mongoose.model("collectionsModel").create({exerciseId,studentId});
    // console.log(data);
    let data = await mongoose.model("collectionsModel").create(req.body);
    if(data!=""){
        res.send({code:200})
    }else{
        res.send({code:-1})
    }
}


module.exports = {
    getAllCollections,
    removeCollection,
    addCollection
}